<template>
	<van-form>
	  <van-cell-group inset>
		<van-field
		  v-model="formData.time"
		  readonly
		  name="time"
		  label="时间:"
		>
			<template #input>
				<u--input readonly v-model="formData.time"></u--input>
			</template>
		</van-field>
		<van-field
		  :colon="true"
		  v-for="item in formClom"
		  :key="item.key"
		  v-model="formData[item.key]"
		  :name="item.key"
		  :label="item.label"
		  :rules="[{ required: true,message: '请输入数字!' }]"
		>
			<template #input>
				<u--input type="number" :placeholder="item.label" v-model="formData[item.key]"></u--input>
			</template>
			<template #button>
			  <text>元</text>
			</template>
		</van-field>
	  </van-cell-group>
	  <view style="margin: 16px;margin-bottom: 10px;">
		<van-button block type="success" native-type="submit"  @click="onSubmit">
			保存
		</van-button>
	  </view>
	  <up-toast ref="uToastRef"></up-toast>
	</van-form>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { onLoad } from '@dcloudio/uni-app'
import { precisionCalc, getLocalData, setLocalData } from '@/utils/util'
import { defaultData, formClom } from '@/utils/config.js'
// #ifdef APP || APP-ANDROID
// 组件丢失问题处理
import UpToast from '@/uni_modules/uview-plus/components/u-toast/u-toast.vue'
// #endif
const uToastRef = ref(null)
const formData = reactive(Object.assign({}, defaultData))

const onSubmit = () => {
	setLocalData(formData).then( res => {
		uToastRef.value.show({
			type: "success",
			title: "完成",
			message: "保存成功",
			complete() {
				uni.$emit('update',{time:formData.time})
				uni.navigateBack();
			},
		})
	}).catch( err => {
		uToastRef.value.show({
			type: "error",
			title: "提示",
			message: err
		})
	})
}

const setFormData = (data) => {
	for (let key in data) {
		if (formData.hasOwnProperty(key)) {
			formData[key] = data[key];
		}
	}
}

onLoad((query)=>{
	const localData = getLocalData(query.time)
	setFormData(localData)
})

</script>

<style>
.u-input.u-border{
	padding: 0 !important;
	border: none !important;
}
</style>